﻿using System;
using System.Collections.Generic;
using University.Workshop.Model;
using System.Collections.ObjectModel;
using System.Globalization;

namespace University.Workshop.Business
{
    public class RouletteManager
    {
        private Collection<RouletteAngleNumber> List;
        private string SortedNumber;

        public void NewTable()
        {
            List = new Collection<RouletteAngleNumber>();            

            double Add = (360.0 / 38.0);
            double InitialAuxiliarAngle = 360 - (Add / 2);
            double FinalAuxiliarAngle = (Add / 2.0);

            //double Add = 9.47;
            //double InitialAuxiliarAngle = 355.37;
            //double FinalAuxiliarAngle = 4.73;
            for (int Pointer = 0; Pointer < 38; Pointer++)
            {
                List.Add(new RouletteAngleNumber { InitialAngle = InitialAuxiliarAngle, FinalAngle = FinalAuxiliarAngle, Color="", Number="" });                
                InitialAuxiliarAngle = FinalAuxiliarAngle + 0.01;
                FinalAuxiliarAngle = InitialAuxiliarAngle + Add;
            }

            List[0].Color = "Green";
            List[0].Number = "0";

            List[1].Color = "Black";
            List[1].Number = "2";

            List[2].Color = "Red";
            List[2].Number = "14";

            List[3].Color = "Black";
            List[3].Number = "35";

            List[4].Color = "Red";
            List[4].Number = "23";

            List[5].Color = "Black";
            List[5].Number = "4";

            List[6].Color = "Red";
            List[6].Number = "16";

            List[7].Color = "Black";
            List[7].Number = "33";

            List[8].Color = "Red";
            List[8].Number = "21";

            List[9].Color = "Black";
            List[9].Number = "6";

            List[10].Color = "Red";
            List[10].Number = "18";

            List[11].Color = "Black";
            List[11].Number = "31";

            List[12].Color = "Red";
            List[12].Number = "19";

            List[13].Color = "Black";
            List[13].Number = "8";

            List[14].Color = "Red";
            List[14].Number = "12";

            List[15].Color = "Black";
            List[15].Number = "29";

            List[16].Color = "Red";
            List[16].Number = "25";

            List[17].Color = "Black";
            List[17].Number = "10";

            List[18].Color = "Red";
            List[18].Number = "27";

            List[19].Color = "Green";
            List[19].Number = "00";

            List[20].Color = "Red";
            List[20].Number = "1";

            List[21].Color = "Black";
            List[21].Number = "13";

            List[22].Color = "Red";
            List[22].Number = "36";

            List[23].Color = "Black";
            List[23].Number = "24";

            List[24].Color = "Red";
            List[24].Number = "3";

            List[25].Color = "Black";
            List[25].Number = "15";

            List[26].Color = "Red";
            List[26].Number = "34";

            List[27].Color = "Black";
            List[27].Number = "22";

            List[28].Color = "Red";
            List[28].Number = "5";

            List[29].Color = "Black";
            List[29].Number = "17";

            List[30].Color = "Red";
            List[30].Number = "32";

            List[31].Color = "Black";
            List[31].Number = "20";

            List[32].Color = "Red";
            List[32].Number = "7";

            List[33].Color = "Black";
            List[33].Number = "11";

            List[34].Color = "Red";
            List[34].Number = "30";

            List[35].Color = "Black";
            List[35].Number = "26";

            List[36].Color = "Red";
            List[36].Number = "9";

            List[37].Color = "Black";
            List[37].Number = "28";
        }

        public RouletteAngleNumber GetAngle(string angle)
        {
            if (String.IsNullOrEmpty(angle))
            {
                return (List[0]);
            }

            angle = angle.Replace("-", "");
            angle = angle.Replace(",", ".");
                
            try
            {
                double AngleNumber = Convert.ToDouble(angle, CultureInfo.InvariantCulture);                
                double IsInAngle = 360 - AngleNumber - AngleNumber;
                if (IsInAngle < 0)
                {
                    IsInAngle = 360 + IsInAngle;
                }
                string AngleAuxiliar = IsInAngle.ToString(CultureInfo.InvariantCulture);
                AngleAuxiliar = AngleAuxiliar.Replace("-", "");
                IsInAngle = Convert.ToDouble(AngleAuxiliar, CultureInfo.InvariantCulture);

                for (int Pointer = 1; Pointer < 38; Pointer++)
                {
                    if (IsInAngle >= List[Pointer].InitialAngle && IsInAngle <= List[Pointer].FinalAngle)
                    {
                        return(List[Pointer]);
                    }
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                return(List[0]);
            }

            return (List[0]);
        }        

        public long GiveMeTheResult(string sortedNumber, Collection<string> itemsSelected, Collection<string> itemsValue)
        {
            if (String.IsNullOrEmpty(sortedNumber) ||
                itemsSelected == null ||
                itemsValue == null)
            {
                return (0);
            }

            this.SortedNumber = sortedNumber;
            long Price = 0;

            for (int PointerCountItemsSelected = 0; PointerCountItemsSelected < itemsSelected.Count; PointerCountItemsSelected++)
            {
                bool Exit = false;
                int Position = GetPositionListNumbers(this.SortedNumber);
                int Number;
                try
                {
                    Number = Convert.ToInt16(this.SortedNumber, CultureInfo.InvariantCulture);
                }
                catch (FormatException)
                {
                    Number = -1;
                }

                #region Color                
                //Red
                if (String.Equals("Red", itemsSelected[PointerCountItemsSelected]) && Exit == false)
                {                    
                    if (String.Equals(List[Position].Color, "Red"))
                    {
                        Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture);
                    }                    
                    Exit = true;
                }

                //Black
                if (String.Equals("Black", itemsSelected[PointerCountItemsSelected]) && Exit == false)
                {
                    if (String.Equals(List[Position].Color, "Black"))
                    {
                        Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture);
                    }
                    Exit = true;
                }
                #endregion 

                #region EvenOdd
                //Even
                if (String.Equals("Even", itemsSelected[PointerCountItemsSelected]) && Exit == false)
                {
                    if ((Number % 2) == 0)
                    {
                        Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture);
                    }
                    Exit = true;
                }

                //Odd
                if (String.Equals("Odd", itemsSelected[PointerCountItemsSelected]) && Exit == false)
                {
                    if ((Number % 2) != 0)
                    {
                        Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture);
                    }
                    Exit = true;
                }
                #endregion

                #region RangeCount
                //1 to 18
                if (String.Equals("1to18", itemsSelected[PointerCountItemsSelected]) && Exit == false)
                {
                    if (Number >= 1 && Number <= 18)
                    {
                        Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture);
                    }
                    Exit = true;
                }

                //19 ToString 36
                if (String.Equals("19to36", itemsSelected[PointerCountItemsSelected]) && Exit == false)
                {
                    if (Number >= 19 && Number <= 36)
                    {
                        Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture);
                    }
                    Exit = true;
                }                
                #endregion

                #region RangeJump
                //2 to 1 Option 1
                if (String.Equals("2to1_1", itemsSelected[PointerCountItemsSelected]) && Exit == false)
                {
                    for (int Auxiliar = 1; Auxiliar <= 36; Auxiliar = Auxiliar + 3)
                    {
                        if (Number == Auxiliar)
                        {
                            Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture) * 2;
                        }
                    }
                    Exit = true;
                }

                //2 to 1 Option 2
                if (String.Equals("2to1_2", itemsSelected[PointerCountItemsSelected]) && Exit == false)
                {
                    for (int Auxiliar = 2; Auxiliar <= 36; Auxiliar = Auxiliar + 3)
                    {
                        if (Number == Auxiliar)
                        {
                            Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture) * 2;
                        }
                    }
                    Exit = true;
                }

                //2 to 1 Option 3
                if (String.Equals("2to1_3", itemsSelected[PointerCountItemsSelected]) && Exit == false)
                {
                    for (int Auxiliar = 3; Auxiliar <= 36; Auxiliar = Auxiliar + 3)
                    {
                        if (Number == Auxiliar)
                        {
                            Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture) * 2;
                        }
                    }
                    Exit = true;
                }                                
                #endregion

                #region Dozen
                //1 st 12
                if (String.Equals("1st12", itemsSelected[PointerCountItemsSelected]) && Exit == false)
                {
                    for (int Auxiliar = 1; Auxiliar <= 12; Auxiliar++)
                    {
                        if (Number == Auxiliar)
                        {
                            Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture) * 2;
                        }
                    }
                    Exit = true;
                }

                //2 st 12
                if (String.Equals("2nd12", itemsSelected[PointerCountItemsSelected]) && Exit == false)
                {
                    for (int Auxiliar = 13; Auxiliar <= 24; Auxiliar++)
                    {
                        if (Number == Auxiliar)
                        {
                            Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture) * 2;
                        }
                    }
                    Exit = true;
                }

                //3 st 12
                if (String.Equals("3rd12", itemsSelected[PointerCountItemsSelected]) && Exit == false)
                {
                    for (int Auxiliar = 25; Auxiliar <= 36; Auxiliar++)
                    {
                        if (Number == Auxiliar)
                        {
                            Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture) * 2;
                        }
                    }
                    Exit = true;
                }
                #endregion


                if (Exit == true)
                {
                    itemsSelected.RemoveAt(PointerCountItemsSelected);
                    PointerCountItemsSelected--;
                }
            }


            for (int PointerCountItemsSelected = 0; PointerCountItemsSelected < itemsSelected.Count; PointerCountItemsSelected++)
            {
                //Sometimes I need to separe twho numbers.
                //For example 9_12_8_11 -> 9 12 8 11
                String[] StringNumber;
                StringNumber = itemsSelected[PointerCountItemsSelected].Split('_');
                int Number;

                for (int PointerCounterStringNumber = 0; PointerCounterStringNumber < StringNumber.Length; PointerCounterStringNumber++)
                {                    
                    try
                    {
                        Number = Convert.ToInt16(StringNumber[PointerCounterStringNumber], CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                        Number = -1;
                    }

                    if (String.Equals(this.SortedNumber, StringNumber[PointerCounterStringNumber]))
                    {
                        #region NumberForPosition
                        if (StringNumber.Length == 1)
                        {
                            Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture) * 34;
                        }
                        if (StringNumber.Length == 2)
                        {
                            Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture) * 17;
                        }
                        if (StringNumber.Length == 3)
                        {
                            Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture) * 11;
                        }
                        if (StringNumber.Length == 4)
                        {
                            Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture) * 8;
                        }
                        if (StringNumber.Length == 5)
                        {
                            Price = Price + Convert.ToInt32(itemsValue[PointerCountItemsSelected], CultureInfo.InvariantCulture) * 6;
                        }
                        #endregion
                    }
                }
            }           
            return (Price);
        }



        private int GetPositionListNumbers(string Number)
        {
            for (int Position = 0; Position < List.Count; Position++)
            {
                if (String.Equals(List[Position].Number, Number))
                {
                    return (Position);
                }
            }
            return (-1); //Not Found
        }
    }
}
